package P150;

import java.util.Stack;

public class P51 {

    public int evalRPN(String[] tokens) {
        Stack<Integer> stack = new Stack<>();
        for (String token : tokens) {
            if (token.equals("+")) {
                int newVal = stack.pop() + stack.pop();
                stack.push(newVal);
            } else if (token.equals("-")) {
                int newVal = -1 * stack.pop() + stack.pop();
                stack.push(newVal);
            } else if (token.equals("*")) {
                int newVal = stack.pop() * stack.pop();
                stack.push(newVal);
            } else if (token.equals("/")) {
                Integer pop1 = stack.pop();
                Integer pop2 = stack.pop();
                stack.push(pop2 / pop1);
            } else {
                stack.push(Integer.parseInt(token));
            }
        }
        return stack.pop();
    }

}
